|
|
|
|
|
|
|
the polygon. Because the points in the polygon all lie in the same plane, they all have the same surface normal vector N. |
|
|
|
|
|
|
|
|
In that case, the intensity at all points in the polygon is roughly the same. Using these facts, you can easily add a simple shading model to the hidden surface removal programs described in previous chapters. For each polygon that must be drawn, use the previous formula to compute the intensity of a point on the polygon. Compute the corresponding color and fill the polygon with that color. |
|
|
|
|
|
|
|
|
Just as you can add a point's red, green, and blue intensities to get its color, you can also add the effects of multiple light sources. Calculate the color component values due to each light source separately, add them, and use the RGB function to calculate the final color value. |
|
|
|
|
|
|
|
|
After you add the components due to several light sources, the result may be greater than 255. If you pass a value greater than 255 into the RGB function, your program crashes. To avoid crashing, test the color components and make sure none are too big before you call RGB. |
|
|
|
|
|
|
|
|
To make it easier to manage light sources, you can create a LightSource class. This class holds the light source's location and intensities. |
|
|
|
 |
|
|
|
|
Option Explicit
Public X As Single
Public Y As Single
Public Z As Single
Public Ir As Single
Public Ig As Single
Public Ib As Single |
|
|
|
|
|
|
|
|
Now you can pass a collection of LightSource objects to a Face3d object. The face can use the light sources' locations and intensities to calculate its color. The SurfaceColor function shown in the following code performs this calculation. |
|
|
|
 |
|
|
|
|
' Return the proper shade for this face
' due to the indicated light source. Private Function SurfaceColor( _
ByVal light_sources As Collection) As Long
Dim light As LightSource
Dim Lx As Single
Dim Ly As Single
Dim Lz As Single
Dim L_len As Single
Dim Nx As Single
Dim Ny As Single
Dim Nz As Single
Dim NdotL As Single
Dim R As Integer |
|
|
|
|
|